#!/bin/sh /etc/rc.common
# Copyright (C) 2010-2011 OpenWrt.org

START=60
STOP=10

SERVICE_DAEMONIZE=1
SERVICE_WRITE_PID=1
SERVICE_STOP_TIME=9

error() {
	echo "${initscript}:" "$@" 1>&2
}

start() {
	diff /etc/my.cnf /usr/share/mysql-ac/my.cnf >/dev/null 2>&1 
	if [ $? -ne 0 ]; then 
		cp -a /usr/share/mysql-ac/my.cnf /etc/my.cnf
	fi  
	local datadir=$(sed -n -e "s/^[[:space:]]*datadir[[:space:]]*=[[:space:]\"']*\([^[:space:]\"']*\)[[:space:]\"']*/\1/p" /etc/my.cnf)
	test -d $datadir || mkdir -p $datadir
	
	local tmpdir=$(sed -n -e "s/^[[:space:]]*tmpdir[[:space:]]*=[[:space:]\"']*\([^[:space:]\"']*\)[[:space:]\"']*/\1/p" /etc/my.cnf)
	test -d $tmpdir || mkdir -p $tmpdir
	
	if [ ! -f "$datadir/mysql/tables_priv.MYD" ]; then
		mysql_install_db --force
		test $? -eq 0 || return 1
		service_start /usr/bin/mysqld
		for i in 1 2 3 4 5 6 7 8 9; do 
			sleep 1
			pidof mysqld >/dev/null 2>&1 
			if [ $? -eq 0 ]; then 
				mysqladmin -u root password 'wjrc0409'
				sync
				return 0
			fi
		done 

		return 1
	fi
	
	service_start /usr/bin/mysqld
}

stop() {
	service_stop /usr/bin/mysqld
}
